
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LPV_POS;
public partial class frmDM_TIENGIO4 : Form
{
    public string ma = "";//PK_: giá trị này dc truyền từ bảng danh sách, trường hợp EDIT
    DataTable tbMainDS = new DataTable();
    DataTable tbMain = new DataTable();
    Boolean iloadform = false;

    #region"    frmDM_TIENGIO4"
    public frmDM_TIENGIO4()
    {
        InitializeComponent();
    }
    #endregion

    #region"    frmDM_TIENGIO4_Load"
    public void frmDM_TIENGIO4_Load(object sender, EventArgs e)
    {
        
        btnluu.Enabled = false;
        btnxoa.Enabled = false;
        this.showcombobox();
        this.showdataDS();
        iloadform = true;
        //ToolTip tt = new ToolTip();
        //tt.SetToolTip(lblSO_GIO_LAM_TRON_VAO,"Vào bao nhiêu tiếng thì dc LÀM TRÒN thành 1 ngày (so với 12h trưa, nếu giờ vào < 12h)");
        //tt.SetToolTip(lblSO_GIO_LAM_TRON_RA,"Ra bao nhiêu tiếng thì dc LÀM TRÒN thành 1 ngày (so với 12h trưa, nếu giờ ra > 12h)");
    }
    #endregion

    #region"    các hàm load dữ liệu"
    #region"    showdata"
    void showdata()
    {

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMain = othuvien.SP_SelectDataTable("sp_DM_TIENGIO4",
                           new string[] { "@flag", "@OBJID" },
                           new object[] { 4, ma });

    }
    #endregion
    #region"    showdataDS"
    void showdataDS()
    {
        string[] param = new string[1];
        object[] value = new object[1];
        int ipos = 0;
        param[ipos] = "@flag";
        value[ipos] = 4;

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMainDS = othuvien.SP_SelectDataTable("sp_DM_TIENGIO4", param, value);
        grdDM_TIENGIO4.DataSource = tbMainDS;
        grdDM_TIENGIO4.Refresh();
        
    }
    #endregion
    #region"    showinfo"
    void showinfo()
    {

        this.showdata();
        if (tbMain.Rows.Count == 0)
        {
            numT2.Value = 0;
            chk_tinh_USD.Checked = false;
            return;
        }

        numT2.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["T2"]).Replace("0-", "-"));

        //TINH_GIA_USD
        Boolean bTINH_GIA_USD = false;
        Boolean.TryParse(tbMain.Rows[0]["TINH_GIA_USD"] + "", out bTINH_GIA_USD);
        chk_tinh_USD.Checked = bTINH_GIA_USD;
        ///
        cboDM_BAN_LOAI_MA.SelectedValue = tbMain.Rows[0]["DM_BAN_LOAI_MA"];

        //DM_TIENGIO4_THEM_GIO
        //string sma = ma;
        //if (ma == "-1" || ma +""== "")
        //    sma = "0";
        //DataTable tbDM_TIENGIO4_THEM_GIO = new thuvien().SP_SelectDataTable("sp_DM_TIENGIO4_THEM_GIO",
        //                                                                    new string[] { "@flag", "@DM_TIENGIO4_OBJID" },
        //                                                                    new object[] { 4, sma });
        
        //grdDM_TIENGIO4_THEM_GIO.DataSource = tbDM_TIENGIO4_THEM_GIO;
        //grdDM_TIENGIO4_THEM_GIO.Refresh();
    }
    #endregion
    #region"    showcombobox"
    void showcombobox()
    {
        thuvien oclsData = new thuvien();

        DataTable tbDM_BAN_LOAI = oclsData.SP_SelectDataTable("sp_DM_TIENGIO4", new string[] { "@flag" }, new object[] { 6 });
        cboDM_BAN_LOAI_MA.DataSource = tbDM_BAN_LOAI;
        if (tbDM_BAN_LOAI.Rows.Count > 0)
            cboDM_BAN_LOAI_MA.SelectedIndex = 0;

    }
    #endregion
    #endregion

    #region"    các sự kiện _Click của các nút THÊM - SỬA- TÌM KIẾM - THOÁT - tbpDanhSach"
    #region"    btnDSthem_Click"
    private void btnDSthem_Click(object sender, EventArgs e)
    {
        tbMain = tbMainDS.Copy();
        tbMain.Clear();
        ma = "-1";
        this.showinfo();
        this.setStatusButton(1);
    }

    #endregion
    #region"    btnDSthoat_Click"
    private void btnDSthoat_Click(object sender, EventArgs e)
    {
        //if (MessageBox.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
        {
            foreach (Form frm in this.MdiParent.MdiChildren)
                if (frm.Name == "frmDM_TIENGIO2")
                    frm.Dispose();
            this.Dispose();
        }

    }
    #endregion

    #endregion

    #region"    các sự kiện _Click của các nút LƯU - XÓA - SỬA  "
    #region"    tao_ma_tu_dong"
    void tao_ma_tu_dong()
    {
    }

    #endregion
    #region"    btnluu_Click"
    private void btnluu_Click(object sender, EventArgs e)
    {
        if (this.save() > 0)
        {
            MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.showdataDS();
        }
    }

    #endregion
    #region"    save"
    int save()
    {
        int _iReasult = -1;
        if (this.ValidateSave())
        {
            int iMA_LOAIDICHVU = 2;//Hotel = 2

            thuvien othuvien = new thuvien();//lớp truy xuat du lieu
            string iOutPut = "@output";
            _iReasult = othuvien.SP_Update("sp_DM_TIENGIO4",
                 new string[] { "@flag", "@OBJID", "@MA_LOAIDICHVU", 
                                "@T2",
                                "@DM_BAN_LOAI_MA","@TINH_GIA_USD"},
                 new object[] { 1, ma, iMA_LOAIDICHVU,
                                numT2.Value, 
                                cboDM_BAN_LOAI_MA.SelectedValue,chk_tinh_USD.Checked },
                                ref iOutPut
                                );
            if (iOutPut == "")
            {
                MessageBox.Show("Lưu thất bại");
                return -1;
            }
        }
        return _iReasult;
    }

    #endregion
    #region"    ValidateSave"
    Boolean ValidateSave()
    {
        return true;
    }

    #endregion
    
    #region"    delete"
    int delete()
    {
        thuvien othuvien = new thuvien();//lớp truy xuat du lieu    
        int _iReasult = othuvien.SP_Update("sp_DM_TIENGIO4",
                                 new string[] { "@flag", "@OBJID" },
                                 new object[] { 3, ma, });
        return _iReasult;
    }

    #endregion

    #region"    btnxoa_Click"
    private void btnxoa_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Bạn muốn xóa", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
            if (this.delete() > 0)
            {
                MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ma = "-1";
                this.showdataDS();
                this.setStatusButton(4);
            }
        }
    }

    #endregion
    
    #region "    setStatusButton"
    void setStatusButton(int i1New2Edt3Sch4Del5Sav)
    {
        btnDSthem.Enabled = i1New2Edt3Sch4Del5Sav != 1;
        btnxoa.Enabled = (i1New2Edt3Sch4Del5Sav != 1 && i1New2Edt3Sch4Del5Sav != 4 && i1New2Edt3Sch4Del5Sav != 3);

        btnluu.Enabled = i1New2Edt3Sch4Del5Sav != 5;
    }
    #endregion

    private void tabDM_TIENGIO4_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (tabDM_TIENGIO4.SelectedTab == tabPage1)
        {//gio
            this.Addform(new frmDM_TIENGIO2());
            this.Hide();
        }
        else if (tabDM_TIENGIO4.SelectedTab == tabPage2)
        {//ngay
            this.Addform(new frmDM_TIENGIO3());
            this.Hide();
        }
    }
    #endregion

    #region "   Addform"
    private void Addform(Form f)
    {
        bool exist = false;

        Form[] frms = this.MdiParent.MdiChildren;
        foreach (Form frm in frms)
        {
            if (frm.Name == f.Name)
            {
                exist = true;
                //frm.WindowState = FormWindowState.Maximized;
                frm.Show();
                frm.Activate();
                break;
            }
        }

        if (!exist)
        {
            f.MdiParent = this.MdiParent;
            f.Show();
            f.WindowState = FormWindowState.Maximized;
        }
    }


    #endregion

    private void frmDM_TIENGIO4_Activated(object sender, EventArgs e)
    {
        tabDM_TIENGIO4.SelectedTab = tbpDanhSach;
        //this.WindowState = FormWindowState.Maximized;
    }


    private void grvDM_TIENGIO4_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
    {
        if (iloadform )
        {
            if (grvDM_TIENGIO4.FocusedRowHandle >= 0)
            {
                iloadform = false;
                ma = "-1";
                DataRow dr = grvDM_TIENGIO4.GetDataRow(grvDM_TIENGIO4.FocusedRowHandle);
                if (dr != null)
                {
                    ma = dr["OBJID"] + "";
                    this.setStatusButton(2);
                }
                this.showinfo();
                iloadform = true;
            }
        }
    }

    private void grvDM_TIENGIO4_Click(object sender, EventArgs e)
    {
        if (grvDM_TIENGIO4.RowCount <= 1)
            this.grvDM_TIENGIO4_FocusedRowChanged(null, null);
    }

    //private void btnThem_ChiTiet_Click(object sender, EventArgs e)
    //{
    //    DataTable tbDM_TIENGIO4_THEM_GIO = (DataTable)grdDM_TIENGIO4_THEM_GIO.DataSource;
    //    DataRow dr = tbDM_TIENGIO4_THEM_GIO.NewRow();
    //    dr["STT"] = tbDM_TIENGIO4_THEM_GIO.Rows.Count + 1;
    //    dr["TRUOC_GIO"] = 0;
    //    dr["TRUOC_TIEN"] = 0;
    //    dr["SAU_GIO"] = 0;
    //    dr["SAU_TIEN"] = 0;
    //    tbDM_TIENGIO4_THEM_GIO.Rows.Add(dr);
    //    grdDM_TIENGIO4_THEM_GIO.DataSource = tbDM_TIENGIO4_THEM_GIO;
    //    grdDM_TIENGIO4_THEM_GIO.Refresh();
    //}
}

